package com.itheima.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.wms.common.service.CrudServiceImpl;
import com.itheima.wms.common.utils.RelationUtils;
import com.itheima.wms.dao.*;
import com.itheima.wms.entity.CheckListEntity;
import com.itheima.wms.entity.OwnerEntity;
import com.itheima.wms.entity.OwnerLocationEntity;
import com.itheima.wms.entity.StockEntity;
import com.itheima.wms.service.OwnerService;
import com.itheima.wms.vo.OwnerDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 业务实现类
 * 货主管理
 * </p>
 */
@Slf4j
@Service
public class OwnerServiceImpl extends CrudServiceImpl<OwnerMapper, OwnerEntity> implements OwnerService {

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private OwnerLocationMapper ownerLocationMapper;

    private static List<Class> SERVICE = new ArrayList() {{
        add(StockMapper.class);
        add(ReceiptMapper.class);
        add(OutboundMapper.class);
        add(CheckMapper.class);
    }};

    @Override
    public List<OwnerEntity> list(Map data) {
        if (data.containsKey("areaId")) {
            Long areaId = Long.valueOf(data.get("areaId").toString());
            LambdaQueryWrapper<OwnerLocationEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OwnerLocationEntity::getAreaId, areaId);
            List<OwnerLocationEntity> ownerLocationEntities = ownerLocationMapper.selectList(wrapper);
            Set<Long> ownerIdSet = ownerLocationEntities.stream().map(ownerLocationEntity -> ownerLocationEntity.getOwnerId()).collect(Collectors.toSet());
            if (CollectionUtils.isEmpty(ownerIdSet)) {
                return new ArrayList<>(0);
            }
            data.put("in_id", ownerIdSet);
            data.remove("areaId");
        }
        return super.list(data);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        idList.forEach(id -> {
            RelationUtils.checkRelation(id, "owner_id", SERVICE);
        });
        return super.removeByIds(idList);
    }

    @Override
    public OwnerDetailVO getDetail(Long id) {
        OwnerDetailVO ownerDetailVO = new OwnerDetailVO();
        OwnerEntity ownerEntity = this.getById(id);
        if (ownerEntity == null) {
            return ownerDetailVO;
        }
        BeanUtils.copyProperties(ownerEntity, ownerDetailVO);

        QueryWrapper<StockEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(sum(total),0) as totalNum")
                .eq("owner_id", ownerEntity.getId());

        Map<String, Object> map = stockMapper.selectMaps(queryWrapper).get(0);
        Integer totalNum = ((java.math.BigDecimal) map.get("totalNum")).intValue();
        ownerDetailVO.setGoodsTotal(totalNum);

        return ownerDetailVO;
    }
}

